/*
 *  运行goovy build.groovy 集成工程在svn上路径 打包的包后缀
 *  如：groovy build.groovy bxn-intergration-uic/release/r_6.0.1  6.0.1-RELEASE
 *  集成工程在svn上路径是用来记录最终的包是用那个集成工程打出来的
 *  包后缀和配置文件中的main.@name共同组成最后war包的名称(用“-”来连接)
 *  最后生成的war包在 temp/target目录下
 */


if(args.length != 2){
	throw new IllegalArgumentException("Please input source version and target version.");
}
println args[0]

def srcVersion = args[0]
def version = args[1] 

//1.读取配置文件

def cfg = new XmlParser().parse(new File("./package-config.xml"))

//println cfg.@svn

def mainProperties = cfg.main[0]
def svnProperties = cfg.svn[0]

ant = new AntBuilder()
//ant.taskdef(name:"svn", classname:"org.tigris.subversion.svnant.SvnTask")  
//<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="svn.classpath" /> 
ant.typedef(resource:"org/tigris/subversion/svnant/svnantlib.xml")
ant.svnSetting(id:"svn.setting",username:svnProperties.@username, password:svnProperties.@password,javahl:false)

ant.delete(dir:"./temp");
ant.mkdir(dir:"./temp");

//2.下载源代码

ant.svn(refid: "svn.setting"){
	cfg.projects.project.each{prj->
		export (srcUrl:"${svnProperties.@url}/${prj.@name}/${prj.@dir}",destPath:"./temp/src/${prj.@name}",revision:"HEAD")
	}
}

//3.编译打包
def p1 = "mvn clean package  -DskipTests=true".execute()
println "${p1.text}"

cfg.projects.project.each{prj->
	def p2 = "mvn clean package -DskipTests=true -f temp/src/${prj.@name}/pom.xml".execute()
	println "${p2.text}"
}

//4.把子工程的classes下的内容压缩为jar包
//TODO 先删除ehcache.xml?
cfg.projects.project.each{prj->
	ant.zip(destfile:"temp/src/${prj.@name}/target/${prj.@name}.jar",basedir:"temp/src/${prj.@name}/target/classes")
}

//5.解压main.war到一个war目录
def mainWarName = findWarNameInDir("./target")
ant.unzip(src:"./target/${mainWarName}.war", dest:"temp/target/war")

//6.把子工程下的文件拷贝到main的war目录下
cfg.projects.project.each{prj->
	def prjWarName = findWarNameInDir("temp/src/${prj.@name}/target");
	//jar
	ant.copy(file:"temp/src/${prj.@name}/target/${prj.@name}.jar", tofile:"temp/target/war/WEB-INF/lib/${prj.@name}.jar")
	//resources
	ant.copy(todir:"temp/target/war/resources"){
		ant.fileset(dir:"temp/src/${prj.@name}/target/${prjWarName}/resources")
	}
	//page
	ant.copy(todir:"temp/target/war/WEB-INF/pages"){
		ant.fileset(dir:"temp/src/${prj.@name}/target/${prjWarName}/WEB-INF/pages")
	}
	//menu
	ant.copy(todir:"temp/target/war/WEB-INF/menu"){
		ant.fileset(dir:"temp/src/${prj.@name}/target/${prjWarName}/WEB-INF/menu")
	}
	

	//dbscript
	ant.copy(todir:"temp/target/war/WEB-INF/dbscript"){
		ant.fileset(dir:"temp/src/${prj.@name}/script")
	}
}

//7.把打包配置信息也添加到war中，以便追代码
ant.copy(file:"package-config.xml", tofile:"temp/target/war/META-INF/pacage-config.xml")
new File("./temp/target/war/META-INF/MANIFEST.MF").append("\nSrc-Version:${srcVersion}")

//8.把war目录打包成war文件
ant.zip(destfile:"temp/target/${mainProperties.@name}-${version}.war",basedir:"temp/target/war")

	

/*==================================================================================================
 * 辅助方法
 *==================================================================================================
 */
def findWarNameInDir(dir){
	def files = new File(dir).list({d, f-> f ==~ /.*.war/ } as FilenameFilter)
	return files[0].substring(0, files[0].length()-4)
}
